package zq.cn.baseadapterapp;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by ZQ on 2016/3/23.
 */
public class MainAdapter extends BaseAdapter{
    private List<PicShow> data;
    private Context context;
    public MainAdapter(){}
    public MainAdapter(Context context, List<PicShow> data) {
        this.context = context;
        this.data = data;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return data.get(position).getId();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        //R.layout.activity_main为int类型
        convertView = inflater.inflate(R.layout.activity_main,null);
        //引用bean
        PicShow picShow = data.get(position);
        //View 视图，可以获取layout.xml中的id信息
        ImageView imageView = (ImageView)convertView.findViewById(R.id.pic);
        //设置各种样式，添加自定义数据内容
        imageView.setBackgroundResource(picShow.getPic());
        TextView name = (TextView) convertView.findViewById(R.id.name);
        name.setText(picShow.getName());
        TextView content = (TextView) convertView.findViewById(R.id.content);
        content.setText(picShow.getContent());
        //监听
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("zq", data.get(position).getName());
            }
        });
        content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("zq",data.get(position).getContent());
            }
        });

        return convertView;
    }
}
